﻿using System;
using System.Net;
using System.Text;

namespace ScreenServer
{
    class Program
    {
        static void Main(string[] args)
        {
            var group = IPAddress.Parse("224.5.6.7");
            var port = 5000;
            if (args.Length > 0)
            {
                var server = new MulticastServer(group, port, 3);
                server.Start();
                while (true)
                {
                    var command = Console.ReadLine();
                    if (string.IsNullOrEmpty(command)) continue;
                    if (command == "exit") break;
                    int n;
                    if (!int.TryParse(command, out n)) continue;
                    var buffer = new byte[n];
                    server.Send(buffer, 0, buffer.Length);
                }
                server.Stop();
                return;
            }
            var c1 = new MulticastClient(group, port);
            c1.DataReceived += (s, e) => Console.WriteLine(e.Length);
            c1.Start();
            while (true)
            {
                var command = Console.ReadLine();
                if (string.IsNullOrEmpty(command)) continue;
                if (command == "exit") break;
            }
            c1.Stop();
        }
    }
    public class BufferEventArgs : EventArgs
    {
        public byte[] Buffer { get; private set; }
        public int Offset { get; private set; }
        public int Length { get; private set; }
        public BufferEventArgs(byte[] buffer, int offset, int length)
        {
            Buffer = buffer;
            Offset = offset;
            Length = length;
        }
    }
}
